package cn.tedu.dfxd.controller;

import cn.tedu.dfxd.entity.ArticleComment;
import cn.tedu.dfxd.entity.Item;
import cn.tedu.dfxd.entity.User;
import cn.tedu.dfxd.mapper.ArticleCommentMapper;
import cn.tedu.dfxd.mapper.RoleMapper;
import cn.tedu.dfxd.mapper.UserMapper;
import cn.tedu.dfxd.mapper.UserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author DBX
 * @effect
 * @date 2022/04/22/12:58
 */

@RestController
@CrossOrigin
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    UserMapper mapper;
    @Autowired
    UserRoleMapper userRoleMapper;
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    ArticleCommentMapper articleCommentMapper;


    @GetMapping("/myTeam")
    public List<User> myTeam(){


        List<User> userList =mapper.myTeam();

        for (User user : userList) {
           Integer roleId =  userRoleMapper.findRoleId(user.getId());
           user.setJob(roleMapper.roleName(roleId));
        }
        return userList;
    }


    @GetMapping("/contact")//查询系统管理员
    public List<User> contact(){

        List<User> userList = new ArrayList<>();

        List<Integer> idList = userRoleMapper.contact();
        for (Integer id : idList) {
           User user =  mapper.findIdUser(id);
           user.setJob(roleMapper.roleName(1));
           userList.add(user);
        }

        return userList;
    }

    @PostMapping("/comments")
    public int comments(ArticleComment comment){
        System.out.println(comment);
        comment.setUserId(String.valueOf(mapper.check(comment.getUserName()).getId()));
        return  articleCommentMapper.insertArticleComment(comment);
    }

}
